package com.dsj.gxrd.modules.activity.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dsj.gxrd.modules.activity.domain.dto.RdGroupDto;
import com.dsj.gxrd.modules.activity.domain.entity.RdGroup;
import com.dsj.gxrd.modules.activity.domain.vo.GroupUserVo;
import com.dsj.gxrd.modules.activity.domain.vo.RdGroupVo;
import com.dsj.gxrd.modules.activity.mapper.RdGroupMapper;
import com.dsj.gxrd.modules.activity.mapper.RdGroupUserMapper;
import com.dsj.gxrd.modules.activity.service.RdGroupService;
import com.dsj.gxrd.modules.activity.service.RdGroupUserService;
import com.dsj.gxrd.utils.BaseEntityHandlerUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author songpin566
 * @date 2023/10/09
 **/
@Service
public class RdGroupServiceImpl extends ServiceImpl<RdGroupMapper, RdGroup> implements RdGroupService {

    @Autowired
    private RdGroupMapper rdGroupMapper;

    @Autowired
    private RdGroupUserMapper rdGroupUserMapper;

    @Autowired
    private RdGroupUserService rdGroupUserService;

    @Override
    public List<RdGroupVo> selectRdGroupList(String name, String groupName) {
        List<RdGroup> groupList = rdGroupMapper.selectRdGroupList(name, groupName);
        List<RdGroupVo> list = new ArrayList<>();
        for (RdGroup rdGroup : groupList) {
            RdGroupVo rdGroupVo = BeanUtils.copyProperties(rdGroup, RdGroupVo.class);
            String ids = rdGroup.getIds();
            if(StringUtils.isNotEmpty(ids)){
                List<Long> collect = Arrays.stream(ids.split(",")).mapToLong(Long::parseLong).boxed().collect(Collectors.toList());
                rdGroupVo.setGroupUserIds(collect);
            }
            list.add(rdGroupVo);
        }
        return list;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean insertRdGroup(RdGroupDto rdGroupDto) {
        List<Long> groupUserIds = rdGroupDto.getGroupUserIds();
        RdGroup rdGroup = BeanUtils.copyProperties(rdGroupDto, RdGroup.class);
        rdGroup.setDelFlag(0);
        BaseEntityHandlerUtils.fastSetBaseEntity(rdGroup, SecurityUtils.getLoginUser(),false);
        boolean b=save(rdGroup);
        rdGroupUserService.insertRdGroupUser(rdGroup.getRdGroupId(),groupUserIds);
        return b;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateRdGroup(RdGroupDto rdGroupDto) {
        Long rdGroupId = rdGroupDto.getRdGroupId();
        rdGroupUserService.deleteGroupUserById(rdGroupId);
        rdGroupUserService.insertRdGroupUser(rdGroupId,rdGroupDto.getGroupUserIds());
        RdGroup rdGroup = BeanUtils.copyProperties(rdGroupDto, RdGroup.class);
        rdGroup.setDelFlag(0);
        BaseEntityHandlerUtils.fastSetBaseEntity(rdGroup, SecurityUtils.getLoginUser(),true);
        return updateById(rdGroup);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult removes(List<Long> groupIds) {
        if(CollectionUtils.isEmpty(groupIds)){
            return AjaxResult.error("请选择数据再删除!");
        }
        for (Long groupId : groupIds) {
            rdGroupUserService.deleteGroupUserById(groupId);
        }
        return removeByIds(groupIds)?AjaxResult.success("删除成功"):AjaxResult.error();
    }

    @Override
    public List<GroupUserVo> getInfo(Long groupId, String name) {
        return rdGroupMapper.getInfo(groupId,name);
    }

    @Override
    public AjaxResult removeGroupUser(List<Long> groupUserIds) {
        return rdGroupUserMapper.deleteBatchIds(groupUserIds)>0?AjaxResult.success("删除成功"):AjaxResult.error();
    }


}
